package com.alks.function.service.impl.biocontrol.deptwork;


import com.alibaba.excel.util.StringUtils;
import com.alks.common.aop.AutoPageAop;
import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.beanutils.ArrayUtils;
import com.alks.entity.data.entity.PsScheduleHeadCut;
import com.alks.entity.data.entity.PsScheduleLotCut;
import com.alks.entity.data.page.PageRecord;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.entity.exception.ServiceErrorException;
import com.alks.function.data.dto.biocontrol.webwork.SchLineDto;
import com.alks.function.data.dto.biocontrol.webwork.ScheduleCutLotDto;
import com.alks.function.data.request.biocontrol.webwork.ScheduleCutLotRequest;
import com.alks.function.mapper.biocontrol.webwork.PsLineCutMapper;
import com.alks.function.mapper.biocontrol.webwork.PsScheduleHeadCutMapper;
import com.alks.function.mapper.biocontrol.webwork.PsScheduleLotCutMapper;
import com.alks.function.mapper.biocontrol.webwork.ScheduleCutLotMapper;
import com.alks.function.service.biocontrol.deptwork.IScheduleCutLotService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class ScheduleCutLotServiceImpl implements IScheduleCutLotService {
    private final ScheduleCutLotMapper scheduleCutLotMapper;
    private final PsScheduleLotCutMapper lotMapper;
    private final PsLineCutMapper lineMapper;
    private final PsScheduleHeadCutMapper headMapper;



    /**
     * 待安排指令明细
     * @param scheduleCutLot
     * @return
     */
    @Override
    @AutoPageAop
    public ResponseInfo getUnScheduleList(ScheduleCutLotRequest scheduleCutLot) {
        String companyId = UserIdThread.get().getCompanyId();
        scheduleCutLot.setCompanyId(companyId);
        List<ScheduleCutLotDto> list = scheduleCutLotMapper.getUnScheduleList(scheduleCutLot);

        PageInfo pageList = new PageInfo(list);
        HashMap map = new HashMap<>();
        map.put("list", list);
        map.put("total", pageList.getTotal());
        return ResponseInfo.ok(map);
    }

    @Override
    public List<SchLineDto> line() {
        String companyId = UserIdThread.get().getCompanyId();
        return lineMapper.lineQuery(companyId);
    }

    @Override
    @AutoPageAop
    public PageRecord<PsScheduleHeadCut> detailList(ScheduleCutLotRequest request) {
        String companyId = UserIdThread.get().getCompanyId();
        String ordType = request.getOrdType()==null||request.getOrdType().equals(1)?"":" DESC";
        LocalDateTime now = LocalDateTime.now();
        String ordField = StringUtils.isBlank(request.getOrdField())?"":","+request.getOrdField();
        String lotOrd = StringUtils.isBlank(request.getLotNo())?"":"IF(LOT_NO = '" + request.getLotNo() + "',0,1),";
        String brandOrd = StringUtils.isBlank(request.getBrand())?"":"IF(BRAND = '" + request.getBrand() + "',0,1),";
        String styleOrd = StringUtils.isBlank(request.getStyle())?"":"IF(STYLE = '" + request.getStyle() + "',0,1),";
        String lineOrd = StringUtils.isBlank(request.getLastLine())?"":"IF(LAST_LINE = '" + request.getLastLine() + "',0,1),";
        /*获取数据*/
        LambdaQueryWrapper<PsScheduleHeadCut> wrapper = new LambdaQueryWrapper<PsScheduleHeadCut>()
                .like(StringUtils.isNotBlank(request.getLotNo()),PsScheduleHeadCut::getLotNo,request.getLotNo())
                .like(StringUtils.isNotBlank(request.getLastLine()),PsScheduleHeadCut::getLastLine,request.getLastLine())
                .like(StringUtils.isNotBlank(request.getStyle()),PsScheduleHeadCut::getStyle,request.getStyle())
                .like(StringUtils.isNotBlank(request.getBrand()),PsScheduleHeadCut::getBrand,request.getBrand())
                .in(ArrayUtils.isNoEmpyt(request.getLineList()),PsScheduleHeadCut::getLastLine,request.getLineList())
                .ge(request.getBeginDate()!=null,PsScheduleHeadCut::getDeliveryDate,request.getBeginDate())
                .le(request.getEndDate()!=null,PsScheduleHeadCut::getDeliveryDate,request.getEndDate())
                .le(request.getLotType()!=null&&request.getLotType().equals(1),PsScheduleHeadCut::getDeliLtDate,now)
                .le(request.getLotType()!=null&&request.getLotType().equals(2),PsScheduleHeadCut::getLastLtDate2,now)
                .le(request.getStockFlag()!=null&&request.getStockFlag().equals(2),PsScheduleHeadCut::getMtlStatus,"OK")
                .last("ORDER BY " +
                        lotOrd+
                        brandOrd+
                        styleOrd+
                        lineOrd+
                        "LOT_NO"+ ordField+ordType
                );
        List<PsScheduleHeadCut> cutList = headMapper.selectList(wrapper);
        PageRecord<PsScheduleHeadCut> record = new PageRecord<>(cutList);
        if (!request.getPageNum().equals(-1)&&!request.getPageSize().equals(-1)){
            Page<PsScheduleHeadCut> page = (Page<PsScheduleHeadCut>) cutList;
            record.setTotal(page.getTotal());
        }
        return record;
    }

    @Override
    public void sch(List<String> lotNoList, String line, BigDecimal seqNo) {
        String companyId = UserIdThread.get().getCompanyId();
        /*判断是否建档*/

        /*获取基础数据*/
        LambdaQueryWrapper<PsScheduleLotCut> lotWrapper = new LambdaQueryWrapper<PsScheduleLotCut>()
                .in(PsScheduleLotCut::getLotNo,lotNoList)
                .eq(PsScheduleLotCut::getCompanyId,companyId);
        List<PsScheduleLotCut> lotCutList = lotMapper.selectList(lotWrapper);
        if (ArrayUtils.isEmpyt(lotCutList)){
            throw new ServiceErrorException("指令未更新，请先【更新指令】");
        }
        /*开始排产*/



    }

    @Override
    public void cancelSch(String line, String seqNo) {
        String companyId = UserIdThread.get().getCompanyId();

    }

    @Override
    public void tempUp() {
        String companyId = UserIdThread.get().getCompanyId();

    }

    @Override
    public void schUp() {
        String companyId = UserIdThread.get().getCompanyId();

    }

    @Override
    public void schUd(String line, List<BigDecimal> seqNoList, Integer type) {
        String companyId = UserIdThread.get().getCompanyId();

    }

    @Override
    public void schMoveLine(String line, List<BigDecimal> seqNoList, String tagLine) {
        String companyId = UserIdThread.get().getCompanyId();

    }


}
